<?php

/**
 * Enter description here...
 *
 * 
 * @version $Id: UserController.php 62 2009-07-22 04:45:58Z littlexiang521 $
 */

class UserController extends Controller {
    
    public function indexAction () {
        if (! $username = $_SESSION['username']) {
            $form = $this->_buildLoginForm();
        } else {
            $form = 'Welcome,' . $username;
        }
        
        $this->form = $form;
        
        return 'user/index';
    }
    
    public function loginAction () {
        $form = $this->_buildLoginForm();
        if ($_POST) {
            $form->setValue($_POST);
            if ($form->isValid()) {
                $info = $form->getValue();
            }
            $user = new UserModel();
            $result = $user->userLogin($info);
            
            if ($result->is_deleted == 1) {
                $msg = array(
                    'message' => '用户已被禁用');
            } elseif ($result) {
                session_regenerate_id(true);
                $_SESSION['user_id'] = $result->id;
                $_SESSION['username'] = $result->username;
                $msg = array(
                    'message' => '登录成功');
            } else {
                $msg = array(
                    'message' => '登录失败');
            }
            $this->redirect('user/index', $msg);
        } else {
            return $this->forward('user/index');
        }
    }
    
    /**
     * @return Form
     */
    private function _buildLoginForm () {
        $form = new Form('user_login', array(
            'method' => 'post' , 
            'action' => $this->makeUri('user/login')));
        
        $input = new Form_Input('username', array(
            'type' => 'text' , 
            'label' => '用户名'));
        $filter = new Filter();
        $filter->addFilter(new Filter_LowerCase());
        $input->addFilter($filter);
        
        $form->addElement($input);
        
        $form->addElement(new Form_Input('password', array(
            'type' => 'password' , 
            'label' => '密码')));
        
        $form->addElement(new Form_Input('submit', array(
            'type' => 'submit'), '提交'), true);
        $form->addElement(new Form_Input('reset', array(
            'type' => 'reset'), '重置'), true);
        return $form;
    }
    
    public function registerAction () {
        $form = $this->_buildRegForm();
        $form->setValue($_POST);
        if (! $_POST) {
            $this->form = $form;
            $this->msg = Context::getParam('msg');
            
            return 'user/register';
        } elseif ($form->isValid()) {
            $form->setValue($_POST);
            $info = $form->getValue();
            
            $user = new UserModel();
            
            if ($user->checkUserExist($info['username'])) {
                $this->redirect('user/register', array(
                    'msg' => '用户已存在'));
            }

            $uploader = new Uploader('icon');
            $icon = $uploader->upload();
            
            $info['icon'] = $icon ? $icon['file'] : '';
            $last_insert_id = $user->addUser($info);
            
            if ($last_insert_id) {
                session_regenerate_id(true);
                $_SESSION['user_id'] = $last_insert_id;
                $_SESSION['username'] = $info['username'];
                $this->redirect('user/index');
            } else {
                $this->redirect('user/register', array(
                    'msg' => '注册失败'));
            }
        }
    }
    /**
     * @return Form
     */
    private function _buildRegForm () {
        $form = new Form('user_login', array(
            'method' => 'post' , 
            'action' => $this->makeUri('user/register') , 
            'enctype' => 'multipart/form-data'));
        
        $input = new Form_Input('username', array(
            'type' => 'text'));
        $filter = new Filter();
        $filter->addFilter(new Filter_LowerCase());
        $input->addFilter($filter);
        
        $form->addElement($input);
        
        $form->addElement(new Form_Input('password', array(
            'type' => 'password')));
        
        $form->addElement(new Form_Input('icon', array(
            'type' => 'file')));
        
        $form->addElement(new Form_Input('submit', array(
            'type' => 'submit'), '提交'), true);
        $form->addElement(new Form_Input('reset', array(
            'type' => 'reset'), '重置'), true);
        return $form;
    }
}
?>